package my.client.service;

import java.util.List;

import my.client.model.AddVehicleHistoryRequest;
import my.client.model.AddVehicleHistoryResponse;
import my.client.model.VehHistSearchCriteria;
import my.client.model.Vehicle;
import my.client.model.VehicleHistorySearchRequest;
import my.client.model.VehicleHistorySearchResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.client.core.WebServiceTemplate;

/**
 * VehicleHistoryServiceDelegatorImpl is an implementation of {@link VehicleHistoryServiceDelegator}.
 * 
 * @author Nilaksh Bajpai.
 */
public class VehicleHistoryServiceDelegatorImpl implements
		VehicleHistoryServiceDelegator {

	private WebServiceTemplate webServiceTemplate;

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * @see my.client.service.VehicleHistoryServiceDelegator#getVehicleHistory(my.client.model.VehHistSearchCriteria)
	 */
	public List<Vehicle> getVehicleHistory(VehHistSearchCriteria criteria) {
		VehicleHistorySearchRequest request = new VehicleHistorySearchRequest();
		request.setCriteria(criteria);
		logger.debug("request:{}", request);
		logger.info("Before sending message");
		VehicleHistorySearchResponse response = (VehicleHistorySearchResponse) webServiceTemplate.marshalSendAndReceive(request);
		logger.debug("Response: {}", response);
		List<Vehicle> vehicles = null;
		if (response != null) {
			vehicles = response.getVehicles();
		}
		return vehicles;
	}


	/**
	 * @see my.client.service.VehicleHistoryServiceDelegator#addVehicleHistory(my.client.model.Vehicle)
	 */
	public void addVehicleHistory(Vehicle vehicle) {
		logger.debug("Vehicle:{}", vehicle);
		logger.info("Before sending message");
		AddVehicleHistoryRequest request = new AddVehicleHistoryRequest();
		request.setVehicle(vehicle);
		AddVehicleHistoryResponse response = (AddVehicleHistoryResponse) webServiceTemplate.marshalSendAndReceive(request);
		logger.debug("Response {}", response);
	}
	
	/**
	 * @param webServiceTemplate
	 */
	public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
		this.webServiceTemplate = webServiceTemplate;
	}
}
